package com.xl.generator.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.CompetitionDetail;
import com.xl.generator.domain.ReviewRecord;
import com.xl.generator.dto.ReviewRecordDetail;
import com.xl.generator.service.*;
import com.xl.generator.mapper.ReviewRecordMapper;
import org.python.util.PythonInterpreter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;

/**
* @author Nannan
* @description 针对表【review_record(评审表)】的数据库操作Service实现
* @createDate 2022-11-12 22:31:51
*/
@Service
public class ReviewRecordServiceImpl extends ServiceImpl<ReviewRecordMapper, ReviewRecord>
    implements ReviewRecordService{
    @Resource
    private CommonUserService commonUserService;

    @Resource
    private CompetitionSchemeService competitionSchemeService;
    @Resource
    private SubjectiveQuestionService subjectiveQuestionService;
    @Resource
    private CompetitionDetailService competitionDetailService;

    @Override
    public ReviewRecordDetail resolveReviewRecord(ReviewRecord entity) {
        ReviewRecordDetail reviewRecordDetail = new ReviewRecordDetail();
        reviewRecordDetail.setReviewRecord( entity);
        reviewRecordDetail.setSubmitUser(commonUserService.getById(entity.getCommonUserId()));
        reviewRecordDetail.setReviewUser(commonUserService.getById(entity.getReviewUserId()));
        reviewRecordDetail.setScheme(competitionSchemeService.getById(entity.getCompetitionSchemeId()));
        reviewRecordDetail.setCreateUser(commonUserService.getById(reviewRecordDetail.getScheme().getCreateUserId()));
        reviewRecordDetail.setCompetitionDetail(competitionDetailService.getById(reviewRecordDetail.getScheme().getCompetitionId()));
        reviewRecordDetail.setQuestionA(subjectiveQuestionService.getById(entity.getQuestionAId()));
        reviewRecordDetail.setQuestionB(subjectiveQuestionService.getById(entity.getQuestionBId()));
        return reviewRecordDetail;
    }
    @Override
    public String executePython(String code ) {

        if (code==null){
            code="";
        }
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
        ByteArrayOutputStream errorStream = new ByteArrayOutputStream(1024);
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.setOut(baoStream);
        interpreter.setErr(errorStream);
        try{
            //我在这里使用相对路径，注意区分
            interpreter.exec(code);
            String strMsg = baoStream.toString();
            return strMsg;
        }
        catch (Exception e){
            e.printStackTrace();
            return errorStream.toString();
        }

    }
}




